package com.crb.crm.manage.message.token;


import java.util.Map;

 class VariableTokenHandler implements TokenHandler {
        private final Map<String,String> variables;
        private final boolean    enableDefaultValue;
        private final String     defaultValueSeparator;

        public VariableTokenHandler(Map<String,String> variables) {
            this.variables = variables;
            this.enableDefaultValue = false;
            this.defaultValueSeparator = ":";
        }
        @Override
        public String handleToken(String content) {
            if (variables != null) {
                String key = content;
                if (enableDefaultValue) {
                    final int separatorIndex = content.indexOf(defaultValueSeparator);
                    String defaultValue = null;
                    if (separatorIndex >= 0) {
                        key = content.substring(0, separatorIndex);
                        defaultValue = content.substring(separatorIndex + defaultValueSeparator.length());
                    }
                    if (defaultValue != null) { 
                        return String.valueOf(variables.get(key));
                    }
                }
                if (variables.containsKey(key)) {
                    return String.valueOf(variables.get(key));
                }
            }
            return "${" + content + "}"; 
        }
    }